<?php include_once "top.php";
if($_GET['id']){
	$product=$db->ig2_want('mae_prize','id='.$_GET['id']);
	if(!$product) tellgoto('访问有误','products.php');
}
?>
<div class="addleft">
	<a href="products.php">商品列表</a>
	<a href="product_edit.php">添加商品</a>
	<a href="ex_record.php">兑换记录(卡券)</a>
	<a href="ex_record2.php">兑换记录(实物)</a>
</div>
<div class="addbar">
	<ul>
	<h2><?php echo $product?'修改商品信息':'添加商品'?></h2>
	<form method="post" action="ra.php" onsubmit="return checkForm(<?php echo $product?1:2?>)" enctype="multipart/form-data">
		<li><label>商品名称：</label><input id="name" type="text" name="name" value="<?php echo $product['name']?>" class="ipt" /></li>
		<li><label>所需积分：</label><input id="need_integral" type="text" name="need_integral" value="<?php echo $product['need_integral']?>" class="ipt" /></li>
		<?php if(!$product){?>
		<li><label>商品类型：</label>
			<input id="is_virtual1" type="radio" name="is_virtual" value="0"<?php echo $product?($product['is_virtual']==0?' checked':''):' checked'?> class="stock" /> 实物商品
			<input id="is_virtual2" type="radio" name="is_virtual" value="1"<?php echo $product?($product['is_virtual']==1?' checked':''):''?> class="stock" /> 虚拟商品
		</li>
		<?php }else{?>
		<li><label>商品类型：</label><span><?php switch($product['is_virtual']){
			case 0 : echo '实物商品'; break;
			case 1 : echo '虚拟商品'; break;
		}?></span></li>
		<?php }?>
		<li><label>商品状态：</label>
			<input id="status1" type="radio" name="status" value="0"<?php echo $product?($product['status']==0?' checked':''):' checked'?> /> 上架
			<input id="status2" type="radio" name="status" value="1"<?php echo $product?($product['status']==1?' checked':''):''?> /> 下架
		</li>
		<?php if(!$product || ($product && $product['is_virtual']==0)){?>
		<div id="stockdiv">
		<li><label>商品库存：</label><input id="stock" type="text" name="stock" value="<?php echo $product['stock']?>" class="ipt" /></li>
		</div>
		<?php }?>
		<?php if($product){?>
		<input type="hidden" name="id" value="<?php echo $product['id']?>" />
		<li><label>现有封图：</label><img src="../prize/<?php echo $product['img']?>" style="width:100px" /></li>
		<?php }?>
		<li><label><?php echo $product?'修改':'商品'?>封图：</label><input id="img" type="file" name="img" value="" /></li>
		<li><label>商品描述：</label>
			<textarea id="describes" style="width:80%;height:150px"><?php echo $product['describes']?></textarea>
			<input type="hidden" id="describesInput" name="describes" value="<?php echo $product['describes']?>" />
		</li>
		<li style="border:none"><label>&nbsp;</label><input type="submit" name="<?php echo $product?'product_edit':'product_add'?>" value="<?php echo $product?'确认修改商品信息':'确认添加商品'?>" /></li>
	</form>
	</ul>
</div>
<script language="javascript">
$('.stock').click(function(){
	if(this.value==0){
		$('#stockdiv').html('<li><label>商品库存：</label><input id="stock" type="text" name="stock" value="" class="ipt" /></li>');
	}else{
		$('#stockdiv').html('');
	}
});

function trim(str){
	return str.replace(/(^\s*)|(\s*$)/g, "");
}
function checkForm(type){
	
	var describes=$("#describes").val().replace(/\n/g,"<br>");//将换行符转为<br>
	$("#describesInput").val(describes);
	
	if(trim(document.getElementById('name').value) == ''){
		alert('请输入商品名称');
		return false;
	}
	var need_integral=document.getElementById('need_integral').value;
	var myregneed_integral = /^\+?[1-9][0-9]*$/;
	if(trim(need_integral) == ''){
		alert('请输入兑换所需积分');
		return false;
	}else{
		if(!myregneed_integral.test(need_integral)){
			alert('请输入正确的商品兑换积分');
			return false;
		}
	}
	if((!document.getElementById('is_virtual1').checked) && (!document.getElementById('is_virtual2').checked)){
		alert('请选择商品类型');
		return false;
	}
	if(type == 2){
		if(trim(document.getElementById('img').value) == ''){
			alert('请选择商品封面图片');
			return false;
		}
	}
	return true;
}
</script>
<?php include_once "down.php";?>